/*
 * Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.android.fbreader;

import android.app.Activity;
import android.view.Gravity;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.fbreader.FBView;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.text.view.ZLTextSelection;


public class SelectionPopupWindow extends PopupWindow {
	
	private int winWidth, winHeight;

	public SelectionPopupWindow(Activity activity, FrameLayout root, Location location, boolean fillWidth) {
		super(activity, root, location, fillWidth);

		ViewTreeObserver vto = getViewTreeObserver();
		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

			@Override
			public boolean onPreDraw() {
				final FBReaderApp fbReader = (FBReaderApp) FBReaderApp.Instance();
				winWidth = getMeasuredWidth();
				winHeight = getMeasuredHeight();
				//LogUtil.d("ZLFile", "winWidth:" + winWidth + " winHeight:" + winHeight);
				if (fbReader != null && winWidth > 0 && winHeight > 0) {

					final int dpi = ZLibrary.Instance().getDisplayDPI();
					final int unit = dpi / 120;
					final FBView txtView = fbReader.getTextView();
					ZLTextSelection.Point leftPoint = txtView.getLeftSelectionPoint();
					ZLTextSelection.Point rightPoint = txtView.getRightSelectionPoint();
					if (leftPoint != null && rightPoint != null) {
						int startY = leftPoint.Y - dpi / 10 - unit * 7;
						int endY = rightPoint.Y + dpi / 10 + unit * 7;


						int screenHeight = GlobalApp.getInstance().getScreenHeight();
						FrameLayout.LayoutParams p = (FrameLayout.LayoutParams) getLayoutParams();

						if (startY >= 20 + winHeight) {
							p.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
							p.topMargin = startY - (15 + winHeight);
							p.bottomMargin = 0;
						} else if (endY + winHeight + 15 <= screenHeight) {
							p.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
							p.bottomMargin = 0;
							p.topMargin = endY + 10;
						} else if (endY - unit * 8 - (startY + unit * 8) > winHeight + unit) {
							p.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
							p.bottomMargin = 0;
							p.topMargin = startY + unit * 8;
						} else {
							p.gravity = Gravity.CENTER;
							p.bottomMargin = 0;
							p.topMargin = 0;
						}
						setLayoutParams(p);

					} else {
						//
					}


				}
				return true;
			}
		});

	}
	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

}
